package net.thermetics.fx.controllers;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xquery.XQException;

import net.thermetics.fx.model.Forum;
import net.thermetics.fx.model.ForumThread;
import net.thermetics.fx.model.ForumTopic;
import net.thermetics.fx.services.ForumService;
import net.thermetics.fx.services.ThreadService;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/mobile")
public class MobileController extends AbstractController {
	@Autowired
	private ThreadService threadService;
	
	@Autowired
	private ForumService forumService;
	
	@RequestMapping("home")
	public String home() {
		return "mobile/home";
	}
	
	@RequestMapping("forums")
	public String forumlist(Model model) throws ClientProtocolException, IOException, XQException, ParserConfigurationException {
		List<Forum> forums = forumService.getForums();
		model.addAttribute("forums", forums);
		return "mobile/forumlist";
	}
	
	@RequestMapping("f{forumID}")
	public String forum(@PathVariable String forumID, Model model) throws ClientProtocolException, IOException, ParserConfigurationException, IllegalStateException, XQException {
		List<ForumTopic> topics = forumService.getTopics("f" + forumID);
		model.addAttribute("topics", topics);
		return "mobile/forum";
	}	
	
	@RequestMapping("t{threadID}")
	public String thread(@PathVariable String threadID, Model model) throws ClientProtocolException, IOException, ParserConfigurationException, XQException {
		ForumThread threadObj = threadService.getThread("t" + threadID);
		model.addAttribute("thread", threadObj);
		return "mobile/thread";
	}
	
	@RequestMapping("basic")
	public String basicForumList(Model model) throws ClientProtocolException, IOException, XQException, ParserConfigurationException {
		List<Forum> forums = forumService.getForums();
		model.addAttribute("forums", forums);
		return "mobile/basic/forumlist";
	}
	
	@RequestMapping("basic/f{forumID}")
	public String basicForum(@PathVariable String forumID, Model model) throws IllegalStateException, IOException, XQException, ParserConfigurationException {
		List<ForumTopic> topics = forumService.getTopics("f" + forumID);
		model.addAttribute("forum", "f" + forumID);
		model.addAttribute("topics", topics);
		return "mobile/basic/forum";
	}
	
	@RequestMapping("basic/{forum}/t{threadID}")
	public String basicThread(@PathVariable String forum, @PathVariable String threadID, Model model) throws ClientProtocolException, IOException, ParserConfigurationException, XQException {
		ForumThread threadObj = threadService.getThread("t" + threadID);
		model.addAttribute("parentForum", forum);
		model.addAttribute("threadID", threadID);
		model.addAttribute("thread", threadObj);
		model.addAttribute("nextPage", 2);
		return "mobile/basic/thread";
	}
	
	@RequestMapping("basic/{forum}/t{threadID}/{page}")
	public String basicThread(@PathVariable String forum, @PathVariable String threadID, @PathVariable Integer page, Model model) throws ClientProtocolException, IOException, ParserConfigurationException, XQException {
		ForumThread threadObj = threadService.getThread("t" + threadID + "-" + page.toString());
		model.addAttribute("parentForum", forum);
		model.addAttribute("threadID", threadID);
		model.addAttribute("thread", threadObj);
		model.addAttribute("nextPage", ++page);
		return "mobile/basic/thread";
	}
}
